package com.gxa.meiye.portal.advice;

import com.gxa.meiye.core.common.JsonResult;
import com.gxa.meiye.core.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindException;
import org.springframework.validation.Errors;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author chen
 * @date 2022/6/29 10:44
 */
@RestController
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler({BindException.class, MethodArgumentNotValidException.class})
    public JsonResult handleBindException(Errors errors) {
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("0");
        StringBuilder stringBuilder = new StringBuilder();
        return null;
    }

    @ExceptionHandler(Exception.class)
    public Map handleException(BusinessException e){
        //1. 给前端返回异常状态码和统一话术
        //2. 记录日志( log4j2 / slf4j / nohup.out)

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode(e.getExceptionCode().getCode());
        jsonResult.setMsg(e.getExceptionCode().getMessage());
        return jsonResult.getMap();
    }



}
